package com.example.service.impl;

import com.example.mapper.MenuMapper;
import com.example.pojo.Menu;
import com.example.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import static com.example.utils.subMenu.buildChildTree;


@Service
public class MenuServicelmpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getMenuWithSubMenu() {
        List<Menu> rootMenu = menuMapper.getMenu();
        List<Menu>  menuList = new ArrayList<>();
        for (Menu menu : rootMenu) {
            //先找到所有根节点，根节点没有Parentid
            if(menu.getParentid() == 0){
                // 以根节点为起点向下递归查找自己的子类
                Menu menuWithChildren  = buildChildTree(menu , rootMenu);
                menuList.add(menuWithChildren);
            }
        }
        return menuList;
    }
}
